All Questions
35 questions
0votes
0answers
133views
Python Typechecking versus TypedDicts?
From what I understand from this answer, it is not possible to use a typeddict and typechecking in a function. So for example, if one has a function: def some_func(some_int: int, some_dict:...
8votes
5answers
10kviews
Is code written inline faster than using function calls?
I wrote some script in Python that creates a giant 2D matrix (1000x1000 or bigger) and fills it with random numbers. And after that, it goes through every element of the matrix and changes the number ...
3votes
3answers
314views
Referencing transient class attributes
I've just started dipping my feet into OOP. Is it considered bad practice to have classes that reference attributes that depend on another function being called and thus may not exist (version 1)? I'...
13votes
6answers
11kviews
Is it reasonable to use dictionaries instead of arguments?
In python I often see functions with a lot of arguments. For example: def translate(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p): // some code return(x, y, z) I like this pattern in some ...
-2votes
1answer
170views
How do you use ad hoc polymorphism/function overloading with functions in Python?
So, let's say you've got a function foobar() which can function with a variable number of parameters inputted into it, and has different behavior for each of them. How do you get this to function ...
0votes
2answers
1kviews
Loop outside method or method with internal loop?
If I have a list of objects that need to have an operation performed on each, is there a best practice in abstracting the loop or not? Looping over list and call def func(item): some_op(item) ...
0votes
1answer
527views
Python: Function pipeline with multiple return/input values, or use OOP? Best Practices?
I have a 'processing' function and a 'serializing' function. Currently the processor returns 4 different types of data structures to be serialized in different ways. Looking for the best practise on ...
1vote
1answer
2kviews
When should an argument be set to None in Python?
The focus of my question is on design. If I have an argument that can be None and it is passed between several functions until finally being used, which function should treat it as a default argument? ...
1vote
2answers
2kviews
Python function name convention for "convert foo to bar", e.g., foo_to_bar, foo2bar
I have a function that converts args given by argparse to a filename: def args2filename(args, prefix): filename = prefix kwargs = vars(args) for key, value in sorted(kwargs.items()): ...
-1votes
2answers
800views
How to decide if a global variable is used inside or outside a function in Python?
In Python variables that are created outside of a function are known as global variables. However to create a global variable inside a function (a local variable), you can use the global keyword. My ...
1vote
1answer
315views
Python - Paradigm to compute different formulas with the same function
I have different equations to compute the same quantity. For example, val = my_func(a, b, c) but also val = my_func(x, y), where my_func represents the quantity I would like to compute: it could be ...
-4votes
4answers
3kviews
Creating one function for multiple purposes vs multiple functions for one purpose each [closed]
I have one function that is used to compute distances of an object in 3 different ways. Is one of the following two methods considered better practice: Creating 3 different functions, one each for ...
5votes
3answers
12kviews
Changing large number of if-elif-else statements to use underlying structure
I have a function that looks something like this: function_name(step, ... , typ): if typ == 'some type of calc method': if step == 1: do_me_at_step_1(...) elif step ...
-1votes
2answers
183views
How to choose the most suitable solution for a problem given some choices?
To ilustrate my main concern let's start by considering a "trivial" typical problem, data filtering & parsing coming from a process and dumping the information onto something {gui console, file, ...
-5votes
2answers
5kviews
Looking For Information about coloring/highlighting output text in python
one of the first modules I’m working on in a python program I’m putting together is a journal. I’m getting comfortable using the print function, but for this project it would be great if i Could ...